using System;
using System.Reflection;

namespace UnitTesting.Fixturing
{
	class MethodVisibilityChecker
	{
		readonly Visibilities _visibilities;

		MethodBase _methodBase;

		internal MethodVisibilityChecker(Visibilities visibilities)
		{
			_visibilities = visibilities;
		}

		internal bool IsSatisfyVisible(MethodBase methodBase)
		{
			_methodBase = Ensure.IsNotNull(() => methodBase);
			return IsSatisfyPublic()
			       || IsSatisfyInternal()
			       || IsSatisfyProtected()
			       || IsSatisfyPrivate();
		}

		bool IsSatisfyPublic()
		{
			return (_visibilities & Visibilities.Public) == Visibilities.Public
			       && _methodBase.IsPublic;
		}

		bool IsSatisfyInternal()
		{
			return (_visibilities & Visibilities.Internal) == Visibilities.Internal
			       && (_methodBase.IsAssembly || _methodBase.IsFamilyOrAssembly);
		}

		bool IsSatisfyProtected()
		{
			return (_visibilities & Visibilities.Protected) == Visibilities.Protected
			       && (_methodBase.IsFamily || _methodBase.IsFamilyOrAssembly);
		}

		bool IsSatisfyPrivate()
		{
			return (_visibilities & Visibilities.Private) == Visibilities.Private
			       && _methodBase.IsPrivate;
		}
	}
}